﻿using SSIS2014To2012;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

namespace SSIS2014Downgrade
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSelectFolder_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                txbOutputFolder.Text = folderBrowserDialog1.SelectedPath;
        }

        private void btnAddPackage_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                foreach (string file in openFileDialog1.FileNames)
                    lbPackages.Items.Add(file);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(lbPackages.Items.Count > 0 && lbPackages.SelectedIndex > -1)
                if(MessageBox.Show("Remove package from conversion list?","Remove",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
                    lbPackages.Items.Remove(lbPackages.SelectedItem);
        }

        private bool ValidateBeforeExecute()
        {
            if (lbPackages.Items.Count < 1)
            {
                MessageBox.Show("You haven't selected any packages to downgrade.", "Downgrade Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return false;
            }

            if (txbOutputFolder.Text.Trim().Length == 0)
            {
                MessageBox.Show("You haven't selected an output folder for the downgraded packages.", "Downgrade Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return false;
            }

            return true;
        }

        private void btnConvert_Click(object sender, EventArgs e)
        {
            if(ValidateBeforeExecute())
            {
                MessageBox.Show("About to downgrade packages. The source packages will remain as 2014 packages and new downgraded copies made.", "Downgrade", MessageBoxButtons.OK, MessageBoxIcon.Information);
                backgroundWorker1.RunWorkerAsync();
            }
        }

       
        void converter_StatusUpdated(object sender, ConversionEventArgs e)
        {
            backgroundWorker1.ReportProgress(100, e.StatusMessage);
        }
        
        private void Form1_Load(object sender, EventArgs e)
        {
            lblVersion.Text = String.Format("Version {0}", Application.ProductVersion);
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            if(MessageBox.Show("Are you sure?","Exit",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
                this.Close();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            SSISConverter converter = new SSISConverter();
            converter.OutputFolder = txbOutputFolder.Text;
            converter.StatusUpdated += converter_StatusUpdated;

            //start downgrading the packages!!
            foreach (var package in lbPackages.Items)
            {
                converter.ConvertPackage(package.ToString());
                backgroundWorker1.ReportProgress(100, " ");
            }    
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            string msg = (string)e.UserState;
            lbStatus.Items.Add(msg);
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
                MessageBox.Show(String.Format("An error occurred: {0}",e.Error.Message),"Downgrade Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
            else
                MessageBox.Show("Downgrade complete. Please check the Downgrade Status information to see if all packages downgraded successfully.", "Downgrade Complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void linkWebSite_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start("http://www.curnow.biz");
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start("http://www.twitter.com/pcurnow");
        }

        

        
        
    }
}
